package com.ujson.chat.client.builder;

import com.ujson.chat.client.session.ClientSession;
import com.ujson.chat.common.proto.MessageProto;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.atomic.AtomicLong;

/**
 * <h3>ujson-parent</h3>
 * <p>消息的公共部分构造器</p>
 *
 * @author JerryLiu  a1031757312@gmail.com
 * @version 1.0
 * @date 2021-11-29 21:50
 */
public abstract class AbstractBuilder {
    private static final AtomicLong sequnce = new AtomicLong(1);
    private ChannelHandlerContext ctx;

    public abstract MessageProto.MessageType getType();

    public long nextSequence() {
        return sequnce.incrementAndGet();
    }

    public MessageProto.Message commonBuild(Channel channel) {
        this.ctx = ctx;
        String sessionId = ClientSession.getClientSession(channel).getSessionId();

        return MessageProto.Message.newBuilder()
                .setType(getType())
                .setSequence(nextSequence())
                .setSessionId(sessionId)
                .buildPartial();
    }
}
